//
//  AppDelegate.swift
//  status
//
//  Created by 彳亍 on 2017/3/22.
//  Copyright © 2017年 彳亍. All rights reserved.
//

import UIKit
import UserNotifications

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        
        // 1. 设置根控制器
        setupRootVC()
        //2. APNs
        setupAPNsNotificaion()
                
        return true
    }

}

// MARK: - 初始化
fileprivate extension AppDelegate {
    
    /// 设置根控制器
    func setupRootVC() {
        // 1. 初始化窗口
        window = UIWindow(frame: UIScreen.main.bounds)
        // 2. 设置根控制器
        window?.rootViewController = CHMainController()
        // 3. 设置主窗口并可见
        window?.makeKeyAndVisible()
    }
 
    
    
}


// MARK: - apns
extension AppDelegate {

    /// APNS
    fileprivate func setupAPNsNotificaion() {
        
        if #available(iOS 10.0, *) {
            UNUserNotificationCenter.current().requestAuthorization(options: [UNAuthorizationOptions.alert,
                                                                              UNAuthorizationOptions.badge,
                                                                              UNAuthorizationOptions.carPlay,
                                                                              UNAuthorizationOptions.sound
            ]) { (isSuccess, error) in
                
                isSuccess ? print("APNs授权成功") : print(error ?? "error")
            }
            
            
        } else {
            
            let userNotificationSetting = UIUserNotificationSettings(types: [UIUserNotificationType.alert,
                                                                             UIUserNotificationType.badge,
                                                                             UIUserNotificationType.sound],
                                                                     categories: nil)
            
            
            UIApplication.shared.registerUserNotificationSettings(userNotificationSetting)
            
        }
        
        
    
    }
    
    
    
}
